<template>
  <el-dialog title="导入" :model-value="modelValue" @close="closed" width="22%">
    <el-upload
    ref="uploadRef"
    class="upload-demo"
    :action="uploadUrl"
    :auto-upload="false"
    limit = "1"
    >
    <template #trigger>
      <el-button type="primary">选择文件</el-button>
    </template>
    <template #tip>
      <div class="el-upload__tip">
        只允许导入一份文件,格式为excel
      </div>
    </template>
    </el-upload>
    <template #footer>
      <el-button @click="closed">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineProps, defineEmits, ref } from 'vue'
import { ElMessage } from 'element-plus'
defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  uploadUrl: {
    type: String,
    required: true
  }
})

const uploadRef = ref({})

const emits = defineEmits(['update:modelValue'])

const closed = () => {
  emits('update:modelValue', false)
}

/**
 * 确定按钮点击事件
 */
const confirm = () => {
  if (uploadRef.value) {
    uploadRef.value.submit()
    ElMessage.success('导入成功')
    closed()
  } else {
    ElMessage.error('导入失败')
  }
}
</script>

<style lang="scss" scoped>
.content {
  text-align: center;
  .title {
    margin-bottom: 10px;
  }
}
</style>
